//
//  JASetupSwitchCell.m
//  JAGatherAdDemo
//
//  Created by Yc on 2024/1/29.
//

#import "SetupCell.h"
#import <Masonry.h>
#import "Header.h"

@interface SetupCell ()

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UISwitch *switchButton;

@property (nonatomic, strong) UILabel *desLabel;

@end

@implementation SetupCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.switchButton];
    [self.contentView addSubview:self.desLabel];
    [self.switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.desLabel addGestureRecognizer:tap];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.leading.equalTo(self.contentView).offset(15);
    }];
    
    [self.switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.trailing.equalTo(self.contentView).offset(-15);
    }];
    
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.trailing.equalTo(self.contentView).offset(-15);
        make.leading.greaterThanOrEqualTo(self.titleLabel.mas_trailing).offset(15);
    }];
}

- (void)setupType:(SetupType)type
            title:(NSString *)title
         describe:(NSString *)describe
      switchState:(BOOL)state {
    self.titleLabel.text = title;
    self.type = type;
    self.desLabel.text = describe;
    BOOL isSwitch = !(self.type > SetupTypeSplashCustomBottom);
    self.desLabel.hidden = isSwitch;
    self.switchButton.hidden = !isSwitch;
    self.switchButton.on = state;
}

- (void)tapAction:(UITapGestureRecognizer *)gesture {
    if (self.copyBlock) {
        self.copyBlock(self.desLabel.text);
    }
}

#pragma mark - Action

- (void)switchAction:(UISwitch *)sender {
    if (self.switchBlock) {
        self.switchBlock(sender.isOn,self);
    }
}

#pragma mark - Lazy
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        [_titleLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [_titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }
    return _titleLabel;
}

- (UISwitch *)switchButton {
    if (!_switchButton) {
        _switchButton = [[UISwitch alloc] init];
        _switchButton.onTintColor = ThemeColor;
    }
    return _switchButton;
}

- (UILabel *)desLabel {
    if (!_desLabel) {
        _desLabel = [[UILabel alloc] init];
        _desLabel.textColor = ThemeColor;
        _desLabel.font = [UIFont systemFontOfSize:14];
        _desLabel.textAlignment = NSTextAlignmentRight;
        _desLabel.userInteractionEnabled = YES;
        _desLabel.numberOfLines = 2;
    }
    return _desLabel;
}

@end
